using Core;
using UnityEngine;

namespace g_gongjianta
{
    public class UITouch : UIWidget //, IUpdate
    {
        private PointerDragListener listener;

        // public event Action<Vector3> OnRefresh;

        public override void Awake(GameObject go)
        {
            base.Awake(go);

            listener = PointerDragListener.Get(go);
            listener.OnDown += OnDownHandler;
            listener.OnBeginDrag += OnBeginDragHandler;
            listener.OnDrag += OnDragHandler;
            listener.OnEndDrag += OnEndDragHandler;
        }

        public override void Dispose()
        {
            if (IsDisposed) return;
            base.Dispose();

            listener.OnDown -= OnDownHandler;
            listener.OnBeginDrag -= OnBeginDragHandler;
            listener.OnDrag -= OnDragHandler;
            listener.OnEndDrag -= OnEndDragHandler;
            listener = null;
        }

        protected override void OnHide()
        {
            // listener.Reset();
        }

        private void Trigger(Vector3 mousePosition)
        {
            var camera = Camera.main;
            if (camera == null) return;
            var pos = camera.ScreenToWorldPoint(mousePosition);
            ScreenEvent.OnChanged?.Invoke(pos);
        }

        private void OnDownHandler(Vector3 mousePosition)
        {
            // OnDown?.Invoke();
            Trigger(mousePosition);
        }

        private void OnBeginDragHandler(Vector3 mousePosition)
        {
            Trigger(mousePosition);
        }

        private void OnDragHandler(Vector3 mousePosition)
        {
            Trigger(mousePosition);
        }

        private void OnEndDragHandler(Vector3 mousePosition)
        {
            Trigger(mousePosition);
        }
    }
}